package tech.mobi.child.base;


import tech.mobi.child.LoginAct;
import tech.mobi.child.MainAct;
import tech.mobi.child.PortalAct;
import tech.mobi.child.service.MainService;
import tech.mobi.child.setting.HelpMainAct;
import tech.mobi.child.utils.DebugUtil;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;

import com.baidu.mapapi.MapActivity;
import com.mobclick.android.MobclickAgent;

public abstract class BaseAct extends MapActivity {
	@SuppressWarnings("rawtypes")
	private Class clazz = null;
	private ProgressDialog progressDialogBase = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		MainService.allActivity.add(this);
		init();
		DebugUtil.MyLog("Test", this.getClass().getName() + "------onCreate");
		// Log.i("lure",this.getClass().getName());
	}
	public void startProgress(){
		progressDialogBase = ProgressDialog.show(this
				, "", "正在加载...");
	}
	
	public void startProgress(String strShow){
		progressDialogBase = ProgressDialog.show(this
				, "",strShow);
	}
	public void stopProgress(){
		if(progressDialogBase!=null&&progressDialogBase.isShowing()){
			progressDialogBase.cancel();
			progressDialogBase.dismiss();
		}
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();

		DebugUtil.MyLog("Test", this.getClass().getName() + "------onDestroy");
		MainService.allActivity.remove(this);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		DebugUtil.MyLog("Test", "BaseAct   onKeyDown");
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			clazz = this.getClass();
			if (clazz.equals(PortalAct.class)) {
				// ui在ProtalAct的时候，使回退键失效
			} else if (clazz.equals(LoginAct.class)) {
				if (MainService.isRunOfService) {
					if (MainService.isRun) {
						MainService.isRun = false;
					}
					MainService.isRunOfService = false;
					Intent intent = new Intent(this, MainService.class);
					stopService(intent);
				}
				finish();
			}  else if (MainService.finishActivity.contains(clazz.getName())) {
				finish();
			} else if (clazz.equals(HelpMainAct.class)) {
//					// ui在HelpMainAct的时候，使回退键失效
			} else {
				MainAct.mainUI.clickBack();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onPause() {
		DebugUtil.MyLog("Test", this.getClass().getName() + "------onPause");
		super.onPause();
		if (!DebugUtil.isDebug()) {
			MobclickAgent.onPause(this);// Umeng
		}
	}

	@Override
	protected void onResume() {
		DebugUtil.MyLog("Test", this.getClass().getName() + "------onResume");
		super.onResume();
		if (!DebugUtil.isDebug()) {
			MobclickAgent.onResume(this);// Umeng
		}
	}

	@Override
	protected void onStart() {
		DebugUtil.MyLog("Test", this.getClass().getName() + "------onStart");

		super.onStart();
	}

	@Override
	protected void onRestart() {
		DebugUtil.MyLog("Test", this.getClass().getName() + "------onRestart");
		super.onRestart();
	}

	@Override
	protected void onStop() {
		DebugUtil.MyLog("Test", this.getClass().getName() + "------onStop");
		super.onStop();
	}

	/**
	 * 设置View是否有效
	 * 
	 * @param view
	 * @param b
	 */
	public void setValid(View view, boolean b) {
		view.setClickable(b);
		view.setEnabled(b);
	}

	public abstract void init();

	public abstract void refresh(Object... param);
	
	/**
	 * 得到版本号
	 */
	public String getClientVersionNumber() {
		String versionName = "";
		PackageManager packageManager = this.getPackageManager();
		try {
			PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
			versionName = packageInfo.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return versionName;
	}
}
